Javascript Testing

Jasmine-Node

Jasmine-node is a node.js module that makes Jasmine framework available in node.js.

Install Jasmine-Node

Install the latest official version with NPM:

sudo npm install jasmine-node -g

Run Tests

Write the tests in *.js files and put them in the /spec folder.

Note: The test files must be named as *spec.js in order for jasmine-node to find them

Run the tests with the command jasmine-node spec/ and the result of the test should show. For example,

jasmine-node spec/

...

Finished in 0.004 seconds
1 test, 1 assertion, 0 failures, 0 skipped

Generate Test Reports

To generate test reports in JUnit format, use --junitreport argument with jasmine-node command.

For example,

Run the command jasmine-node spec/ --junitreport will generate a xml file under /reports.

The folder where the reports are generated can be changed with --output argument. For example,

jasmine-node spec/ --junitreport --output reportfolder/new-test-report.xml

will generate a test report named as new-test-report in reportfolder directory.

--autotest argument provides automatic execution of specs after each change.
Combining with --watch argument can make jasmine-node only re-run tests only if files under specified folders changed.

A Simeple Example

In this example, a simple test is created to test a node module xml2js's builder function.

The builder function creates xml strings from javascript objects. For example,

var xml2js = require('xml2js');
var builder = new xml2js.Builder();
var xmlObj = {
    root: "rootValue"
};

var xmlStr = builder.buildObject(xmlObj);

will generate a string xmlStr from the javascript object xmlObj.

We can then test if xmlStr contains string <root>rootValue</root> to determine if the builder function is working.

expect(xmlStr).toContain("<root>rootValue</root>");

The example project can be downloaded here.

Run the command jasmine-node spec and the following result should show.

Finished in 0.008 seconds
1 test, 4 assertions, 0 failures, 0 skipped

References

Jasmine-Node Github Page

Xml2js Module